<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\common\logic\admin;
use app\common\model\MytaskModel;
use app\common\model\ReviewModel;
use app\common\model\RecordModel;
use app\common\model\TemplateModel;
use app\common\model\TemplateGroupModel;
use app\common\model\RelationStudentModel;
use app\common\logic\home\Relation;
use think\Db;
class Reviews
{
	/**
	 * [获取课题评审列表]
	 * @param  int  $param.add_status [description]
	 * @param  int  $param.user_id [description]
	 * @param  integer $page  [description]
	 * @return [type]         [description]
	 */
	public function getList($param='',$page=20){
		//模糊课题查询
		if(!empty($param['tem_name'])){
			$da = TemplateModel::where('name','like','%'.trim($param['tem_name']).'%')->field('id')->select()->toArray();
			
			$tem_ids = 0;
			if(!empty($da)){
				$ar = [];
				foreach ($da as $key => $value) {
					$ar[] = $value['id'];
				}
				$tem_ids = implode(',', $ar);
			}
			$da = TemplateGroupModel::where('template_id','in',$tem_ids)->field('group_id')->select()->toArray();
			$grou_ids = 0;
			if(!empty($da)){
				$ar = [];
				foreach ($da as $key => $value) {
					$ar[] = $value['group_id'];
				}
				$grou_ids = implode(',', $ar);
			}
			$param['grou_ids'] = $grou_ids;
			
		}
		$data = Db::view('Mytask', '*,status as mytask_status')
				    ->view('Record', 'mytask_id,class_id,t_user_id,s_user_id,status,group_id', 'Record.mytask_id=Mytask.id')
				    ->view('Relation_student', 'user_id,class_id,level', 'Relation_student.class_id=Record.class_id and Relation_student.user_id=Record.s_user_id')
				    ->where('add_status',$param['add_status'])
				    ->where('t_user_id',$param['user_id'])
				    ->where(function($query) use($param){
				    	if(isset($param['grou_ids'])){
				    		$query->where('Record.group_id','in',$param['grou_ids']);
				    	}
				    })
				    ->where('level',1)
				    ->order('id desc')
				    ->paginate($page,false,['query'=>$param])
				    ->each(function($value,$item){

				    	$value['review'] = ReviewModel::where('mytask_id',$value['mytask_id'])->order('review_id desc')->select()->toArray();
				    	$value['review']['count'] = count($value['review']);
				    	$value['review']['refuse_count'] = ReviewModel::where('mytask_id',$value['mytask_id'])->where('refuse_time','<>','')->count();
				    	if($value['status'] == 3){
				    		$value['class'] = RelationStudentModel::relation('appuser')->where('class_id',$value['class_id'])->where('status',3)->order('level desc')->select()->toArray();
				    		$value['class']['count'] = count($value['class']);
				    	}else{
				    		$value['class'] = RelationStudentModel::relation('appuser')->where('class_id',$value['class_id'])->where('user_id',$value['user_id'])->select()->toArray();
				    		$value['class']['count'] = count($value['class']);
				    	}
				    	return $value;
				    });
		return $data;
	}
	/**
	 * [提交课题审核]
	 * @param [type] $mytask_id [description]
	 * @param [type] $user_id   [description]
	 */
	public function add($mytask_id,$user_id)
	{
		$Relation = new Relation();

		$mytask_ids = $Relation->getGrouplist($mytask_id,$user_id);

		//获取课题状态
		$data_mytask = MytaskModel::get($mytask_id);

		if($data_mytask['status'] != 3){
			return [
					'code'=>0,
					'msg'=>'课题未完成,不能提交'
				];
		}
		//获取课题发送信息
		$data_record = RecordModel::where('mytask_id',$mytask_id)->where('s_user_id',$user_id)->find();
		//提交信息添加
		$data = [
				'mytask_id'=>$mytask_id,
				's_user_id'=>$data_record['s_user_id'],
				't_user_id'=>$data_record['t_user_id'],
			];
		$ReviewModel = new ReviewModel();
		$ReviewModel->save($data);
		//修改组成员的课题状态
		foreach ($mytask_ids as $key => $value) {
			$rs = MytaskModel::where('id',$value)->update(['add_status'=>2]);
		}
		
		return ['code'=>1];
	}

	public function agree($mytask_id,$user_id,$evaluate){
		$rs = ReviewModel::where('mytask_id',$mytask_id)->where('s_user_id',$user_id)->where('refuse_time',NULL)->update(['agree_time'=>date('Y-m-d H:i:s')]);
		$Relation = new Relation();
		$mytask_ids = $Relation->getGrouplist($mytask_id,$user_id);
		//修改组成员的课题状态
		foreach ($mytask_ids as $key => $value) {
			$rs = MytaskModel::where('id',$value)->update(['add_status'=>3,'evaluate'=>$evaluate]);
		}
		if($rs){
			return 1;
		}else{
			return 0;
		}

	}
}